/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package technotheist.imgcog.filter;

import technotheist.imgcog.base.Module;

/**
 *
 * @author Mitch
 */
public class LogPolarTransform extends Module {

    @Override
    protected void process() {
        runner().run();
    }

    protected class LogPolarTransformRunner extends Runner {

        final int inputLenX, inputLenY, lenZ;
        final int outputLenX, outputLenY;

        public LogPolarTransformRunner(LogPolarTransformConfig config) {
            super(config);
            inputLenX = config.getInputLenX();
            inputLenY = config.getInputLenY();
            lenZ = config.getLenZ();
            outputLenX = config.getOutputLenX();
            outputLenY = config.getOutputLenY();
        }
        static final float PI = (float) Math.PI;

        @Override
        public void run() {
            int ox = getGlobalId(0);
            int oy = getGlobalId(1);
            int oi = getGlobalId();
            float r = ((float) ox) / outputLenX;
            float theta = (180f / PI) * (((float) oy) / outputLenY);
            int ix0 = (int) (floor(r * cos(theta) * inputLenX));
            int iy0 = (int) (floor(r * sin(theta) * inputLenY));
            int ix1 = (int) (ceil(r * cos(theta) * inputLenX));
            int iy1 = (int) (ceil(r * sin(theta) * inputLenY));
            int ii0 = index3D(ix0, iy0, 0, inputLenY, lenZ);
            int ii1 = index3D(ix1, iy1, 0, inputLenY, lenZ);
            int ii = index3D((ix0 + ix1) / 2, (iy0 + iy1) / 2, 0, inputLenY, lenZ);

            if (ii == ii0 || ii == ii1) {
                for (int z = 0; z < lenZ; z++) {
                    layerNOutput[oi + z] = layer0Nodes[ii + z];
                }
            } else {
                for (int z = 0; z < lenZ; z++) {
                    layerNOutput[oi + z] = (layer0Nodes[ii + z] + layer0Nodes[ii0 + z] + layer0Nodes[ii1 + z]) / 3f;
                }
            }
        }
    }
}
